"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : list_hash二级分区表列约束(主键):约束推迟/using index tablespace
Create At   : 2022/2/17
Owner       : @daiguatutu
Description :
    1.创建二级分区表，一级分区键包含列约束主键
    2.开启事务并设置约束在事务提交时检查,插入重复数据后commit
    3.创建二级分区表，一级分区键包含列约束主键，using index表空间
    4.列出所有名称以t_subpartition_0141开头的表、视图和索引
    5.查看执行计划
    6.创建二级分区表，普通列包含列约束主键，using index表空间
    7.列出所有名称以t_subpartition_0141开头的表、视图和索引
    8.清理环境
Expect      :
    1.成功
    2.插入重复数据成功,commit合理报错
    3.成功
    4.成功,索引表空间为ts_subpartition_0141
    5.成功
    6.成功
    7.成功,索引表空间为ts_subpartition_0141
    8.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0141'
        self.ts_name = 'ts_subpartition_0141'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表，一级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key  deferrable ,col_2 int ," \
            f"col_3 int ,col_4 int) " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"( partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10) " \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3 ), " \
            f"  partition p_list_2 values (1,2,3,4,5,6,7,8,9,10) " \
            f"  ( subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"    subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"    subpartition p_hash_2_5 ), " \
            f"  partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f"  partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f"  ( subpartition p_hash_4_1 ), " \
            f"  partition p_list_5 values (default) " \
            f"  ( subpartition p_hash_5_1 ), " \
            f"  partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f"  ( subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"    subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:开启事务并设置约束在事务提交时检查,插入重复数据后commit ' \
               'expect:插入重复数据成功,commit合理报错-----'
        self.log.info(text)
        sql = "start transaction;" \
            f"set constraints all deferred;" \
            f"insert into {self.t_name} values(111,111),(205,205);" \
            f"insert into {self.t_name} values(111,111),(205,205);" \
            f"commit;"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertEqual(2, msg.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败:' + text)
        self.assertIn(self.constant.unique_index_error_info, msg,
                      '执行失败:' + text)

        text = '-----step3:创建二级分区表，一级分区键包含列约束主键，using index表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key using index tablespace {self.ts_name} ," \
            f"col_2 int ,col_3 int ,col_4 int) " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"( partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10) " \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3 ), " \
            f"  partition p_list_2 values (1,2,3,4,5,6,7,8,9,10) " \
            f"  ( subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"    subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"    subpartition p_hash_2_5 ), " \
            f"  partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f"  partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f"  ( subpartition p_hash_4_1 ), " \
            f"  partition p_list_5 values (default) " \
            f"  ( subpartition p_hash_5_1 ), " \
            f"  partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f"  ( subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"    subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step4:列出所有名称以t_subpartition_0141开头的表、视图和索引 ' \
               'expect:成功,索引表空间为ts_subpartition_0141-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\d+ t_subpartition_0141')
        self.log.info(msg)
        self.assertIn(f'btree (col_1) TABLESPACE {self.ts_name}', msg,
                      '执行失败:' + text)

        text = '-----step5:查看执行计划 expect:成功-----'
        self.log.info(text)
        insert_sql = f"explain select * from {self.t_name} where col_1 = '1';"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(f"Index Scan using t_subpartition_0141_pkey "
                      f"on {self.t_name}", msg, '执行失败:' + text)

        text = '-----step6:创建二级分区表，普通列包含列约束主键，using index表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 int ,col_3 int ," \
            f"col_4 int primary key using index tablespace {self.ts_name}) " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"( partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10) " \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3 ), " \
            f"  partition p_list_2 values (1,2,3,4,5,6,7,8,9,10) " \
            f"  ( subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"    subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"    subpartition p_hash_2_5 ), " \
            f"  partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f"  partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f"  ( subpartition p_hash_4_1 ), " \
            f"  partition p_list_5 values (default) " \
            f"  ( subpartition p_hash_5_1 ), " \
            f"  partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f"  ( subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"    subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step7:列出所有名称以t_subpartition_0141开头的表、视图和索引 ' \
               'expect:成功,索引表空间为ts_subpartition_0141-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\d+ t_subpartition_0141')
        self.log.info(msg)
        self.assertIn(f'btree (col_4) TABLESPACE {self.ts_name}', msg,
                      '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step8:清理环境-----')
        text_1 = '-----删除表和表空间 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop tablespace if exists {self.ts_name};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
